class Solution {
    public int minArray(int[] numbers) {
        if (numbers == null || numbers.length == 0) return -1;
        int left = 0, right = numbers.length - 1, res = -1;
        int mid = left;
        while (numbers[left] >= numbers[right]) {
            mid = (left + right) >> 1;
            if (left + 1 == right) {
                mid = right;
                break;
            }
            if (numbers[mid] == numbers[left] && numbers[mid] == numbers[right]) {
                return find(numbers, left, right);
            }
            if (numbers[mid] >= numbers[left]) {
                left = mid;
            }
            if (numbers[mid] <= numbers[right]) {
                right = mid;
            }
        }
        return numbers[mid];
    }

    private int find(int[] numbers, int left, int right) {
        int res = numbers[0];
        for (int i = left; i <= right; i++) {
            if (res > numbers[i]) res = numbers[i];
        }
        return res;
    }

    private static void printArr(int[] arr, Integer left, Integer right) {
        if (arr == null || arr.length == 0) return;
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        for (int i = 0; i < arr.length; i++) {
            if (left != null && left.equals(i) || right != null && right.equals(i)) {
                sb.append("[").append(arr[i]).append("]");
            } else {
                sb.append(" ").append(arr[i]).append(" ");
            }
        }
        sb.append("}");
        System.out.println(sb);
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.minArray(new int[]{2, 2, 2, 0, 1}));
    }
}